package com.splic.api.controller.product;

import com.github.pagehelper.PageInfo;
import com.splic.common.annotation.Log;
import com.splic.common.core.controller.BaseController;
import com.splic.common.core.domain.ResponseObj;
import com.splic.common.enums.BusinessType;
import com.splic.common.exception.BusinessException;
import com.splic.common.utils.DateUtils;
import com.splic.common.utils.ShiroUtils;
import com.splic.product.domain.*;
import com.splic.product.dto.LocationInfoDTO;
import com.splic.product.service.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * 用户其他信息
 */
@RestController
@RequestMapping("/api/other")
@Slf4j
public class ApiOtherController extends BaseController {

    @Autowired
    private WeatherService weatherServiceImpl;

    @Autowired
    private ExchangeRateService exchangeRateServiceImpl;

    @Autowired
    private HolidayVacationsService holidayVacationsServiceImpl;

    @Autowired
    private TodayInHistoryService todayInHistoryServiceImpl;

    @Autowired
    private ProvinceService provinceServiceImpl;

    @Autowired
    private CityService cityServiceImpl;

    @Autowired
    private CountyService countyServiceImpl;

    @Resource
    private AccuweatherService accuweatherService;

    @Autowired
    private AccuAreaService accuAreaServiceImpl;
    /**
     * 获取天气数据列表
     *
     * @return
     */
    @GetMapping("/getWeather")
    public ResponseObj<List<Weather>> getWeather(@RequestParam("lng")BigDecimal lng,@RequestParam("lat")BigDecimal lat) {
        try{
            if(lng == null || lat == null){
                return ResponseObj.ERROR("经纬度不能为空。");
            }
            Long userId = ShiroUtils.getUserId();
            List<Weather> weatherList = weatherServiceImpl.selectUserWeather(lng,lat,userId);
            return ResponseObj.SUCCESS(weatherList);
        }catch (BusinessException e){
            log.error("获取天气数据失败:{}",e.getMessage());
            return ResponseObj.ERROR(e.getMessage());
        }catch (Exception e){
            log.error("获取天气数据失败:{}",e);
            return ResponseObj.ERROR("获取天气数据失败。");
        }
    }

    /**
     * 获取汇率数据列表
     *
     * @return
     */
    @GetMapping("/getExchangeRateList")
    public ResponseObj<List<ExchangeRate>> getExchangeRateList() {
        try{
            ExchangeRate exchangeRate = new ExchangeRate();
            exchangeRate.setDate(DateUtils.getCurrentDayStartTime());
            List<ExchangeRate> list = exchangeRateServiceImpl.selectExchangeRateList(exchangeRate);
            return ResponseObj.SUCCESS(list);
        }catch (Exception e){
            log.error("获取汇率数据列表失败:{}",e);
            return ResponseObj.ERROR("获取汇率数据列表失败");
        }
    }

    /**
     * 获取节假日数据列表
     *
     * @return
     */
    @GetMapping("/getHolidayVacationsList")
    public ResponseObj<PageInfo<HolidayVacations>> getHolidayVacationsList() {
        try{
            startPage();
            HolidayVacations vacations = new HolidayVacations();
            vacations.setDate(DateUtils.getCurrentDayStartTime());
            List<HolidayVacations> list = holidayVacationsServiceImpl.selectPageList(vacations);
            if (CollectionUtils.isEmpty(list)){
                list = Lists.newArrayList();
                HolidayVacations holidayVacations = new HolidayVacations();
                holidayVacations.setDate(vacations.getDate());
                holidayVacations.setTitle("工作日");
                list.add(holidayVacations);
            }
            return ResponseObj.SUCCESS(new PageInfo(list));
        }catch (Exception e){
            log.error("获取节假日数据列表失败:{}",e);
            return ResponseObj.ERROR("获取节假日数据列表失败");
        }
    }

    /**
     * 获取历史上的今天数据列表
     *
     * @return
     */
    @GetMapping("/getTodayInHistoryList")
    public ResponseObj<PageInfo<TodayInHistory>> getTodayInHistoryList() {
        try{
            startPage();
            TodayInHistory todayInHistory = new TodayInHistory();
            todayInHistory.setDate(DateUtils.getCurrentDayStartTime());
            List<TodayInHistory> list = todayInHistoryServiceImpl.selectPageList(todayInHistory);
            return ResponseObj.SUCCESS(new PageInfo(list));
        }catch (Exception e){
            log.error("获取历史上的今天数据列表失败:{}",e);
            return ResponseObj.ERROR("获取历史上的今天数据列表失败");
        }
    }


    /**
     * 获取省份数据列表
     *
     * @return
     */
    @GetMapping("/getProvinceList")
    public ResponseObj<List<Province>> getProvinceList() {
        try{
            List<Province> list = provinceServiceImpl.selectProvinceList(null);
            return ResponseObj.SUCCESS(list);
        }catch (Exception e){
            log.error("获取省份数据列表失败:{}",e);
            return ResponseObj.ERROR("获取省份数据列表失败");
        }
    }

    /**
     * 获取城市数据列表
     *
     * @return
     */
    @GetMapping("/getCityByProvinceCode")
    public ResponseObj<List<City>> getCityByProvinceCode(String provinceCode) {
        try{
            City city = new City();
            city.setProvinceCode(provinceCode);
            List<City> list = cityServiceImpl.selectCityList(city);
            return ResponseObj.SUCCESS(list);
        }catch (Exception e){
            log.error("获取省份数据列表失败:{}",e);
            return ResponseObj.ERROR("获取省份数据列表失败");
        }
    }

    /**
     * 获取县市数据列表
     *
     * @return
     */
    @GetMapping("/getCountyByCityCode")
    public ResponseObj<List<County>> getCountyByCityCode(String cityCode) {
        try{
            County county = new County();
            county.setCityCode(cityCode);
            List<County> list = countyServiceImpl.selectCountyList(county);
            return ResponseObj.SUCCESS(list);
        }catch (Exception e){
            log.error("获取省份数据列表失败:{}",e);
            return ResponseObj.ERROR("获取省份数据列表失败");
        }
    }

    /**
     * 获取县市数据列表
     *
     * @return
     *//*
    @GetMapping("/getProvinceJsonList")
    public ResponseObj<List<ProvinceJson>> getProvinceJsonList() {
        try{
            List<ProvinceJson> list = provinceServiceImpl.selectProvinceJsonList();
            return ResponseObj.SUCCESS(list);
        }catch (Exception e){
            log.error("获取省份数据列表失败:{}",e);
            return ResponseObj.ERROR("获取省份数据列表失败");
        }
    }*/

    @Autowired
    private ClearUserSubStatusService clearUserSubStatusService;

    /**
     * 获取县市数据列表
     *
     * @return
     */
    @GetMapping("/clearUserSubStatus")
    public ResponseObj clearUserSubStatus() {
        try{
            clearUserSubStatusService.clearUserSubStatus();
            return ResponseObj.SUCCESS();
        }catch (Exception e){
            log.error("获取省份数据列表失败:{}",e);
            return ResponseObj.ERROR("获取省份数据列表失败");
        }
    }

    /**
     * 根据经纬度查询地理位置
     * @Param longitude 经度
     * @Param latitude 纬度
     * @return
     */
    @GetMapping("/getAccuAddress")
    public ResponseObj<LocationInfoDTO> getAddress(@RequestParam("latitude") String latitude, @RequestParam("longitude") String longitude) {
        try{
            if(latitude == null || longitude == null){
                return ResponseObj.ERROR("经纬度不能为空。");
            }
            return ResponseObj.SUCCESS(accuweatherService.getAddressByLatLon(latitude, longitude));
        }catch (Exception e){
            log.error("查询地理位置失败:{}",e);
            return ResponseObj.ERROR("查询地理位置失败");
        }
    }

    /**
     * 获取天气数据列表
     * @Param longitude 经度
     * @Param latitude 纬度
     * @return
     */
    @GetMapping("/getAccuWeather")
    public ResponseObj<List<Weather>> getWeather(@RequestParam("latitude") String latitude, @RequestParam("longitude") String longitude) {
        try{
            if(latitude == null || longitude == null){
                return ResponseObj.ERROR("经纬度不能为空。");
            }
            Long userId = ShiroUtils.getUserId();
            List<Weather> weatherList = accuweatherService.selectUserWeather(latitude,longitude,userId);
            return ResponseObj.SUCCESS(weatherList);
        }catch (BusinessException e){
            log.error("获取天气数据失败:{}",e.getMessage());
            return ResponseObj.ERROR(e.getMessage());
        }catch (Exception e){
            log.error("获取天气数据失败:{}",e);
            return ResponseObj.ERROR("获取天气数据失败。");
        }
    }



    /**
     * 获取区域数据列表
     *
     * @return
     */
    @GetMapping("/getAccuList")
    public ResponseObj<List<AccuArea>> getAccuList(String code) {
        try{
            AccuArea accuArea = new AccuArea();
            accuArea.setParentCode(code);
            List<AccuArea> list = accuAreaServiceImpl.selectAccuList(accuArea);
            return ResponseObj.SUCCESS(list);
        }catch (Exception e){
            log.error("获取省份数据列表失败:{}",e);
            return ResponseObj.ERROR("获取省份数据列表失败");
        }
    }
}
